package com.sx.pipeline.ui.message.detail

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.text.TextUtils
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActMessageDetailBinding
import com.sx.pipeline.ui.message.MessageVM


/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: 消息详情
 */
@Suppress("UNREACHABLE_CODE")
@Route(path = RoutePath.Message.MESSAGE_DETAIL)
class MessageDetailActivity : CYDataBindingBaseActivity<ActMessageDetailBinding, MessageVM>
    (R.layout.act_message_detail),View.OnClickListener {

    @JvmField
    @Autowired(name = "title")
    var name: String? = null

    @JvmField
    @Autowired(name = "content")
    var content: String? = null

    @JvmField
    @Autowired(name = "createTime")
    var createTime: String? = null

    override fun initData() {
        super.initData()
        mBinding.setVariable(BR.onClick, this)
        mBinding.titleBar.leftView.setOnClickListener {
            finish()
        }
        mBinding.tvTitle.text=name
        mBinding.tvTime.text=createTime
        content?.let { initWebView(it) }
    }


    @SuppressLint("SetJavaScriptEnabled")
    private fun initWebView(content: String) {
        val webSettings = mBinding.webview.settings
        webSettings.defaultTextEncodingName = "UTF-8"
        webSettings.javaScriptEnabled = true
        webSettings.domStorageEnabled = true//设置适应Html5的一些方法
        mBinding.webview.loadUrl(
            "javascript:document.getElementsByTagName('body')[0].style.marginBottom = '0px';" +
                    "document.getElementsByTagName('body')[0].style.marginRight = '0px';"
        )
        mBinding.webview.webChromeClient = WebChromeClient()
        mBinding.webview.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                return try {
                        if (url.startsWith("http:") || url.startsWith("https:")) {
                            view.loadUrl(url)
                        } else {
                            val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
                            startActivity(intent)
                        }
                        true
                    } catch (e: Exception) {
                        false
                    }
                return true
            }
        }
        if(content.startsWith("http")){
            mBinding.webview.loadUrl(content)
        }else {
            loadContent(content)
        }
    }

    private fun loadContent(content: String) {
        val data = if (TextUtils.isEmpty(content)) "" else content.replace(
            "<img",
            "<img style=\"max-width:100%;width:auto;height:auto;\""
        )
        mBinding.webview.loadDataWithBaseURL(null, data, "text/html", "UTF-8", null)
    }


    override fun onClick(v: View?) {
        when (v?.id) {

        }
    }

}